<?php

sfContext::getInstance()->getConfiguration()->loadHelpers(array('Helper','Url','JavascriptBase','Asset','Tag','FormCompact10','I18N','Partial')  );


function asset_tag_elem(sfAsset $asset , $width , $height){
    $type = $asset->getType();
    $thumb = $asset->getsfAssetThumbByDim($width , $height);
    $path = $thumb ? $thumb->getWebPath() : sfAssetToolkit::_to_web_image_path($asset->getFullPath()) ;

    if($type == 'image'){
        return image_tag($path , array('width'=>$width , 'height'=>$height));
    }
    elseif($type=='swf'){
        $swf_name = basename($path);
        return <<<EOF
 <object width="{$width}" height="{$height}">
<param name="movie" value="{$path}">
<embed src="{$path}" width="{$width}" height="{$height}">
</embed>
</object>
EOF;
}
}



function auto_wrap_text($text)
{
    return preg_replace('/([_\-\.])/', '<span class="wrap_space"> </span>$1<span class="wrap_space"> </span>', $text);
    return wordwrap($text, 2, '<span class="wrap_space"> </span>', true);
}

/**
 * Gives an image tag for an asset
 * 
 * @param sfAsset $asset
 * @param string $thumbnail_type
 * @param bool $file_system
 * @param array $options
 * @return string
 */
function asset_image_tag($asset, $thumbnail_type = 'full', $options = array(), $relative_path = null)
{
    $options = array_merge(array(
    'alt'   => $asset->getDescription() . ' ' . $asset->getCopyright(),
    'title' => $asset->getDescription() . ' ' . $asset->getCopyright()
    ), $options);

    $other_video_thumb = sfAssetsLibraryTools::getThumbnailPath($asset->getFolderPath(), $asset->getFilename(), $thumbnail_type);
    $other_video_thumb = substr($other_video_thumb ,0 , -4).'.jpg';

    if($asset->isImage())
    {
        $src = $asset->getUrl($thumbnail_type, $relative_path);
        //die($src);
    }elseif(file_exists($other_video_thumb)){
        $src = globals::_to_web_image_path($other_video_thumb) ;

    }
    else
    {
        if($thumbnail_type == 'full')
        {
            throw new sfAssetException('Impossible to render a non-image asset in an image tag');
        }
        else
        {
            switch($asset->getType())
            {
                case 'txt':
                    $src = '/sfAssetsLibraryPlugin/images/txt.png';
                    break;
                case 'xls':
                    $src = '/sfAssetsLibraryPlugin/images/xls.png';
                    break;
                case 'doc':
                    $src = '/sfAssetsLibraryPlugin/images/doc.png';
                    break;
                case 'pdf':
                    $src = '/sfAssetsLibraryPlugin/images/pdf.png';
                    break;
                case 'html':
                    $src = '/sfAssetsLibraryPlugin/images/html.png';
                    break;
                case 'archive':
                    $src = '/sfAssetsLibraryPlugin/images/archive.png';
                    break;
                case 'bin':
                    $src = '/sfAssetsLibraryPlugin/images/bin.png';
                    break;
                default:
                    $src = '/sfAssetsLibraryPlugin/images/unknown.png';
            }
        }
    }
    return image_tag($src, $options);
}

function link_to_asset($text, $path, $options = array())
{
    return str_replace('%2F', '/', link_to($text, $path, $options));
}

function link_to_asset_action($text, $asset)
{
    $tiny_mce = sfContext::getInstance()->getRequest('tinymce') ? '&tinymce=1' : '';
    $user = sfContext::getInstance()->getUser();
    if ($user->hasAttribute('popup', 'sf_admin/sf_asset/navigation'))
    {
        switch($user->getAttribute('popup', null, 'sf_admin/sf_asset/navigation'))
        {
            case 1:
                // popup called from a Rich Text Editor (ex: TinyMCE)
                return link_to($text, "sfAsset/create_thumb?asset_id=".$asset->getId().$tiny_mce, 'title='.$asset->getFilename());
            case 2:
                // popup called from a simple form input (or via input_sf_asset_tag)

                return link_to_function($text, "setImageField('".$asset->getUrl()."','".$asset->getId()."','".url_for('sfAsset/ajax_get_thumbs?asset_id='.$asset->getId())."')");
        }
    }
    else
    {
        // case : sf view (i.e. module sfAsset, view list)
        return link_to($text, "sfAsset/edit?id=".$asset->getId(), 'title='.$asset->getFilename());
    }
}

function init_asset_library()
{
    use_helper('JavascriptBase');
    use_javascript('/sfAssetsLibraryPlugin/js/main', 'last');
    echo javascript_tag('sfAssetsLibrary.init(\''.url_for('sfAsset/list?popup=2').'\')');
}

function object_input_sf_asset_tag($object, $method, $options = array())
{
    $options = _parse_attributes($options);
    $name    = _convert_method_to_name($method, $options);
    $value   = _get_object_value($object, $method);

    return input_sf_asset_tag($name, $value, $options);
}

function input_sf_asset_tag($name, $value, $options = array())
{
    use_helper('FormCompact10', 'I18N');
    use_javascript('/sfAssetsLibraryPlugin/js/main', 'last');
    $options = _convert_options($options);
    $type = 'all';
    if (isset($options['images_only']))
    {
        $type = 'image';
        unset($options['images_only']);
    }
    if(!isset($options['id']))
    {
        $options['id'] = get_id_from_name($name);
    }

    $form_name = 'this.previousSibling.previousSibling.form.name';
    if (isset($options['form_name']))
    {
        $form_name = "'".$options['form_name']."'";
        unset($options['form_name']);
    }

    // The popup should open in the currently selected subdirectory
    $html  = input_tag($name, $value, $options) . '&nbsp;';
    $html .= image_tag('/sfAssetsLibraryPlugin/images/folder_open', array(
    'alt' => __('Insert Image'),
    'style' => 'cursor: pointer; vertical-align: middle',
    'onclick' => "
      initialDir = document.getElementById('".$options['id']."').value.replace(/\/[^\/]*$/, '');
      if(!initialDir) initialDir = '".sfConfig::get('app_sfAssetsLibrary_upload_dir', 'media')."';
      sfAssetsLibrary.openWindow({
        form_name: ".$form_name.",
        field_name: '".$name."',
        type: '".$type."',
        url: '".url_for('sfAsset/list?dir=PLACEHOLDER')."?popup=2'.replace('PLACEHOLDER', initialDir),
        scrollbars: 'yes'
      });"
      ));

      return $html;
}

function init_assets_library_popup()
{
    use_javascript('/sfAssetsLibraryPlugin/js/main', 'last');

    return javascript_tag('sfAssetsLibrary.init(\''.url_for('sfAsset/list').'?popup=2'.'\')');
}

function assets_library_breadcrumb($path, $linkLast = false, $action = '')
{
    $action = $action ? $action : sfContext::getInstance()->getRequest()->getParameter('action');
    if($action == "edit")
    {
        $action = "list";
    }
    $html = '';
    $breadcrumb = explode("/" , $path);
    $nb_dirs = count($breadcrumb);
    $current_dir = '';
    $i = 0;
    foreach ($breadcrumb as $dir)
    {
        if(!$linkLast && ($i == $nb_dirs - 1))
        {
            $html .= $dir;
        }
        else
        {
            $current_dir .= $i ? '/' . $dir : $dir;
            $html .= link_to_asset($dir, 'sfAsset/'.$action.'?dir='.$current_dir) .'<span class="crumb">/</span>';
        }
        $i++;
    }
    return $html;
}